//
//  NSString+Extension.m
//  megawave
//
//  Created by houzhen on 2017/8/24.
//  Copyright © 2017年 multway. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)

+(instancetype)getPathWith:(NSString *)path{
    NSMutableString *docPath =(NSMutableString *) NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"%@",docPath);
    return [docPath stringByAppendingString:path];
}

+ (NSDate *)dateFromString:(NSString *)dateString
{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat: @"yyyy-MM-dd"];
    NSDate *destDate= [formatter dateFromString:dateString];

    return destDate;
}

//NSDate转NSString
+ (NSString *)stringFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat: @"yyyy-MM-dd"];
    
    NSString *destDateString = [formatter stringFromDate:date];
    
    return destDateString;
}

+(NSMutableArray *)getArrayWithPlistPath:(NSString *)path{
    NSString *path1 = [NSString getPathWith:path];
    NSMutableArray *array = [NSMutableArray array];
    NSData *data = [NSData dataWithContentsOfFile:path1];
    if (data!=nil) {
        array = [NSMutableArray arrayWithContentsOfFile:path1];
    }
    else{
        array = [NSMutableArray array];
    }
    return array;
}
//将时间点转化成日历形式

+(NSDate *)getCustomDateWithHour:(NSInteger)hour

{
    
    //获取当前时间
    NSDate * destinationDateNow = [NSDate date];
    
    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *currentComps = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    currentComps = [currentCalendar components:unitFlags fromDate:destinationDateNow];
    
    //设置当前的时间点
    
    NSDateComponents *resultComps = [[NSDateComponents alloc] init];
    
    [resultComps setYear:[currentComps year]];
    
    [resultComps setMonth:[currentComps month]];
    
    [resultComps setDay:[currentComps day]];
    
    [resultComps setHour:hour];
    
    
    
    NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    return [resultCalendar dateFromComponents:resultComps];
    
}
//获取时间段

+(NSString *)getTheTimeGreeting

{
    
    //    NSDate * currentDate = [self getNowDateFromatAnDate:[NSDate date]];
    
    
    
    NSDate * currentDate = [NSDate date];
    
    if ([currentDate compare:[NSString getCustomDateWithHour:0]] == NSOrderedDescending && [currentDate compare:[NSString getCustomDateWithHour:9]] == NSOrderedAscending)
        
    {
        
        return @"早上好";
        
    }
    
    else if ([currentDate compare:[NSString getCustomDateWithHour:9]] == NSOrderedDescending && [currentDate compare:[NSString getCustomDateWithHour:11]] == NSOrderedAscending)
        
    {
        
        return @"上午好";
        
    }
    
    else if ([currentDate compare:[NSString getCustomDateWithHour:11]] == NSOrderedDescending && [currentDate compare:[NSString getCustomDateWithHour:13]] == NSOrderedAscending)
        
    {
        
        return @"中午好";
        
    }
    
    else if ([currentDate compare:[self getCustomDateWithHour:13]] == NSOrderedDescending && [currentDate compare:[self getCustomDateWithHour:18]] == NSOrderedAscending)
        
    {
        
        return @"下午好";
        
    }
    
    else
        
    {
        
        return @"晚上好";
        
    }
    
}

- (NSString *)trainURLEncodedString
{
    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                                    (CFStringRef)self,
                                                                                                    (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",
                                                                                                    NULL,
                                                                                                    kCFStringEncodingUTF8));
    
    return encodedString;
}

+(NSString *)getSexIDCardNumber:(NSString *)num{
    
    int  a = [[num substringWithRange:NSMakeRange(16, 1)] intValue];
    int b = a%2;
    if (b== 1) {
        return @"男";
    }else{
        return @"女";
    }
}

+(NSString *)getAgeIDCardNumber:(NSString *)num{
    NSString *year  = [num substringWithRange:NSMakeRange(6, 4)];
    NSString *month   = [num substringWithRange:NSMakeRange(10, 2)];
    NSString *day   = [num substringWithRange:NSMakeRange(12, 2)] ;
    return [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
}


+(BOOL)valiMobile:(NSString *)mobile{
    if (mobile.length == 11 &&[mobile hasPrefix:@"1"]){
        return YES;
    }else{
        return NO;
    }
}

- (NSString *)formatFloat:(float)f
{
    if (fmodf(f, 1)==0) {//如果有一位小数点
        return [NSString stringWithFormat:@"%.0f",f];
    } else if (fmodf(f*10, 1)==0) {//如果有两位小数点
        return [NSString stringWithFormat:@"%.1f",f];
    } else {
        return [NSString stringWithFormat:@"%.2f",f];
    }
}


@end
